﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using brockredge.CLASSES;

namespace brockredge
{
    public partial class Profile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["userId"] == null)
                Response.Redirect("/NewAccount.aspx");
            if (Page.IsPostBack)
            {
                return;
            }
            getUserDetailFromUserTable();
            getUserDetailFromAddress_DetailTable();
        }

        private void getUserDetailFromAddress_DetailTable()
        {
         
            testDataContext tdc = new testDataContext();
            var AddressQ = from address in tdc.locations
                        where tdc.User_Addresses.Any(ua=>ua.Location_Id==address.location_id && ua.User_Id==(Guid)Session["userId"].ToString().ToGuid())
                        select address;
            if (AddressQ.Count() > 0)
            {
                foreach (var address in AddressQ)
                {
                    lblLine1Des.Text = address.location_name;
                    lin1txtbox.Text = address.location_name;
                    lblLine2Dis.Text = address.landmark;
                    lin2txtbox.Text = address.landmark;
                }
            }
        }
        public void getUserDetailFromUserTable()
        {
            testDataContext tdc = new testDataContext();
            var userQ = from user in tdc.Users
                        where user.user_id == (Guid)Session["userId"].ToString().ToGuid()
                        select user;
            if (userQ.Count() > 0)
            {
                foreach (var user in userQ)
                {
                    lblFnameDis.Text = user.fname;
                    fnametxtbox.Text = user.fname;
                    lblLnameDis.Text = user.lname;
                    lnametxtbox.Text = user.lname;
                    lblMobileDis.Text = user.mobile;
                    //mobileTextBox.Text = user.mobile;
                }
            }
        }

        protected void saveProfile_Click(object sender, EventArgs e)
        {
            testDataContext tdc = new testDataContext();

            updateUser(tdc);

            var addressSelect = from address in tdc.locations
                                where tdc.User_Addresses.Any(ua => ua.Location_Id == address.location_id && ua.User_Id == (Guid)Session["userId"].ToString().ToGuid())
                                select address;
            if(addressSelect.Count()>0)
            {
                updateUserAddress(tdc);
            }
            else
            {
                Guid locationId = (Guid)Session["userId"].ToString().ToGuid();

                insertUserLocation(tdc,locationId);

                insertIntoUser_Address(tdc, locationId);

            }
            Response.Redirect("/Profile.aspx");
            
        }

        private void updateUser(testDataContext tdc)
        {
            /**User Update*/
            User userQ = tdc.Users.Single(user => user.user_id == (Guid)Session["userId"].ToString().ToGuid());

            userQ.fname = fnametxtbox.Text;
            userQ.lname = lnametxtbox.Text;
            userQ.mobile = mobileTextBox.Text;


            //tdc.Users.InsertOnSubmit(userQ);
            tdc.SubmitChanges();
            /** User Update Close*/
        }

        private void insertIntoUser_Address(testDataContext tdc, Guid locationId)
        {
            User_Address ua = new User_Address();
            ua.Location_Id = (Guid)locationId.ToString().ToGuid();
            ua.User_Id = (Guid)Session["userId"].ToString().ToGuid();

            tdc.User_Addresses.InsertOnSubmit(ua);
            tdc.SubmitChanges();
        }

        private void insertUserLocation(testDataContext tdc,Guid locationId)
        {
            location loc = new location();
            loc.location_id = locationId;
            loc.location_name = lin1txtbox.Text;
            loc.landmark = lin2txtbox.Text;


            tdc.locations.InsertOnSubmit(loc);
        }

        private void updateUserAddress(testDataContext tdc)
        {
            string locationId = null;
            var userAddress=from address in tdc.User_Addresses
                                 where address.User_Id==(Guid)Session["userId"].ToString().ToGuid()
                                 select address;
            if (userAddress.Count() > 0)
            {
                foreach (var ua in userAddress)
                {
                    locationId = ua.Location_Id.ToString(); ;
                }

                var userAddressUpdt = (from address in tdc.User_Addresses
                                  where address.User_Id == (Guid)Session["userId"].ToString().ToGuid()
                                  select address).First();
                if(ddlArea.Text!="")
                    userAddressUpdt.Location_Id = (Guid)ddlArea.Text.ToGuid();
                userAddressUpdt.User_Id = (Guid)Session["userId"].ToString().ToGuid();

                tdc.SubmitChanges();
            }
        }
    }
}